package cn.chenhao.quartz.service.impl;

import cn.chenhao.quartz.job.MainJob;
import cn.chenhao.quartz.service.IQuartzService;
import cn.chenhao.quartz.utils.QuartzJobInfo;
import cn.chenhao.quartz.utils.QuartzUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

import java.util.Map;
@Service
public class QuartzServiceImpl implements IQuartzService {

    @Autowired
    private SchedulerFactoryBean bean;


    @Override
    public void addJob(QuartzJobInfo jobInfo) {
        // 添加定时任务所需的参数
        Scheduler scheduler = bean.getScheduler();// 调度器
        String jobName = jobInfo.getJobName();// 执行的任务名字
        Class aClass = MainJob.class;// 公共的作业类
        Object params = jobInfo.getParams();// 参数
        String cronj = jobInfo.getCronj();// cron表达式的时间
        QuartzUtils.addJob(scheduler, jobName, aClass, params, cronj);
    }

    @Override
    public void removeJob(String jobName) {
        System.err.println("==============取消未执行的任务==========");
        QuartzUtils.removeJob(bean.getScheduler(),jobName);
    }
}
